export class handleDate{

    static formatTimestampToDateTime(timestamp: string | number | Date, flag?: number) {
        const date = new Date(timestamp);
        const year = date.getFullYear();
        const month = (date.getMonth() + 1).toString().padStart(2, "0");
        const day = date.getDate().toString().padStart(2, "0");
        const hours = date.getHours().toString().padStart(2, "0");
        const minutes = date.getMinutes().toString().padStart(2, "0");
        const seconds = date.getSeconds().toString().padStart(2, "0");
        if (flag===1) {
          return `${hours}:${minutes}`;
        } else if(flag===2) {
          return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
        }else if(flag===3){
            const now = new Date()
            const nowYear = now.getFullYear();
            const nowMonth = (now.getMonth() + 1).toString().padStart(2, "0");
            const nowDay = now.getDate().toString().padStart(2, "0");
            if(nowYear===year && nowMonth ===month && nowDay===day){
                return `${hours}:${minutes}`
            }else{
                return `${year}/${month}/${day}`
            }
        }
      }
      //   获取之前到现在的时间差
      static formatTime(now: number, timestamp: number) {
        let diff = now - timestamp; // 计算时间差（毫秒）
        // 定义时间单位（毫秒）
        let minute = 60 * 1000;
        let hour = 60 * minute;
        let day = 24 * hour;
        let month = 30 * day;
        let year = 365 * day;
    
        // 根据时间差确定时间单位
        if (diff < minute) {
          let second = Math.floor(diff / 1000) > 0 ? Math.floor(diff / 1000) : 0;
          return second + "秒";
        } else if (diff < hour) {
          let minutes = Math.floor(diff / minute);
          return minutes + "分钟";
        } else if (diff < day) {
          let hours = Math.floor(diff / hour);
          let minutes = Math.floor((diff - hours * hour) / minute);
          return hours + "小时" + minutes + "分钟";
        } else if (diff < month) {
          return Math.floor(diff / day) + "天";
        } else if (diff < year) {
          return Math.floor(diff / month) + "个月";
        } else {
          return Math.floor(diff / year) + "年";
        }
      }

}

